package com.wei.web.middleware.httpfilter.utils;

import org.springframework.core.io.UrlResource;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.Enumeration;

public class FileUtil {

    public static String getContent(String path, ClassLoader classLoader) throws IOException {
        Enumeration<URL> urls = classLoader.getResources(path);
        if (urls.hasMoreElements()) {
            URL url = urls.nextElement();
            UrlResource resource = new UrlResource(url);
            try (InputStream is = resource.getInputStream()) {
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(is));
                StringBuilder stringBuilder= new StringBuilder();
                String line;
                while ((line = bufferedReader.readLine())!= null) {
                    stringBuilder.append(line);
                }
                return stringBuilder.toString();

            } catch (IOException e) {
                throw new RuntimeException(e);
            }

        }
        return null;
    }
}
